#!/bin/bash
#读取配置文件到字典中
function read_config_to_dic() {
	cfg_backups_path=${dic[cfg_backups_path]}
	if [ ! -f "$cfg_backups_path/config" ]; then 
		echo '配置文件不存在'
		exit
	fi
	source "$cfg_backups_path/config"
	dic[packaging_path]=${packaging_path}
	if [ -z "${dic[packaging_path]}" ]; then
		echo '请配置打包路径'
		exit
	fi
	dic[packaging_command_type]=${packaging_command_type}
	if [ -z "${dic[packaging_command_type]}" ]; then
                echo '请配置打包类型'
                exit
	elif [[ "${dic[packaging_command_type]}" = 'tar' ]];then
		dic[packaging_command_prefix]='tar -zcPf '
		dic[packaging_file_suffix]='.tar.gz'
	else 
		echo '只支持tar类型，请配置正确'
		exit
        fi
	dic[packaging_type]=${packaging_type}
	if [ -z "${dic[packaging_type]}" ]; then
                echo '请配置打包类型'
                exit
        fi
	dic[packaging_file_prefix]=${packaging_file_prefix}
	dic[packaging_file_server_name]=${packaging_file_server_name}
	#排除文件夹
	dic[packaging_exclude_folder]=${packaging_exclude_folder}
	dic[enable_remote]=${enable_remote}
	if [ -z "${dic[enable_remote]}" ]; then
		#如果没配置，则默认不开启
		dic[enable_remote]='0'                
        fi
	dic[remote_type]=${remote_type}
	if [ -z "${dic[remote_type]}" ] && [[ "${dic[enable_remote]}" = '1' ]] ; then
                echo '请配置传输类型'
		exit
        elif [[ "${dic[remote_type]}" != 'scp' ]] && [[ "${dic[enable_remote]}" = '1' ]]; then
		echo '传输类型只支持scp'
		exit	
	fi
	#处理scp相关类型的苏剧传输
	if [[ ${dic[remote_type]} = 'scp' ]];then 
		dic[scp_server_name]=${scp_server_name}
	fi
}
#执行打包命令方法
function exec_packaging_command() {
	packaging_command=$1
	packaging_path=${dic[packaging_path]}	
	cd $packaging_path
	#执行打包
	echo "执行打包命令:$packaging_command"
	bash -c "$packaging_command"
	echo '打包完成'
}

#执行整体打包方法
function exec_batch_packaging() {
	echo '执行整体打包方法'
	packaging_path=${dic[packaging_path]}
	packaging_command=${dic[packaging_command_prefix]}
	file_name_suffix=${dic[packaging_file_suffix]}
        file_name_prefix=${dic[packaging_file_prefix]}
	file_name_server_name=${dic[packaging_file_server_name]}
	file_name="$file_name_prefix-$(date +%Y%m%d)-$file_name_server_name$file_name_suffix"
	packaging_command="$packaging_command $file_name "
	#拼接排除目录
	packaging_exclude_folder=${dic[packaging_exclude_folder]}
        if [ -n "$packaging_exclude_folder" ]; then
                array=(${packaging_exclude_folder//:/ })
		for var in "${array[@]}"
           	 do
            		packaging_command="$packaging_command --exclude=$var "
           	 done
        fi
	packaging_command="$packaging_command $packaging_path"
	echo $packaging_command
	exec_packaging_command "$packaging_command"
	#判断打包文件是否已存在
	complate_file_path="$packaging_path/$file_name"
	if test ! -f $complate_file_path ; then
		echo '打包文件失败'
		exit
	fi
	#将打包完成的文件地址保存到字典中
	dic[complate_file_path]=$complate_file_path
}
#执行单个文件夹打包方法
function exec_single_packaging() {
	echo '执行单文件夹打包方法'
	packaging_path=${dic[packaging_path]}
        packaging_prefix_command=${dic[packaging_command_prefix]}
        file_name_suffix=${dic[packaging_file_suffix]}
        file_name_prefix=${dic[packaging_file_prefix]}
        file_name_server_name=${dic[packaging_file_server_name]}
	#处理排除目录
	packaging_exclude_folder=${dic[packaging_exclude_folder]}
	exclude_folder_array=
        if [ -n "$packaging_exclude_folder" ]; then
                exclude_folder_array=(${packaging_exclude_folder//:/ })
        fi
	#列出打包目录下的文件和文件夹信息
	cd $packaging_path
	current_folder_list=`ls $file_path`
        folder_list=(${current_folder_list// /})
	current_complate_file_list=
	for folder_item in ${folder_list[@]}
	  do
		flag='1'
		for exclude_item in ${exclude_folder_array[@]}
		  do
			if [[ "$folder_item" = "$exclude_item" ]]; then
				flag='0'
			fi
		  done
		if [[ $flag = "1" ]]; then
                     file_name="$file_name_prefix-$folder_item-$(date +%Y%m%d)-$file_name_server_name$file_name_suffix"
                     packaging_command="$packaging_prefix_command $file_name $packaging_path/$folder_item"
                     exec_packaging_command "$packaging_command"
		     complate_file_path="$packaging_path/$file_name"
		     if test ! -f $complate_file_path ; then
               		 echo '打包文件失败'
	               	 exit
        	     fi
		     if [ -z "$current_complate_file_list" ]; then 
			current_complate_file_list=$complate_file_path
		     fi
		     current_complate_file_list="$current_complate_file_list,$complate_file_path"                        
                fi      	  
	  done
	#将打包完成的文件地址保存到字典中
        dic[complate_file_path]=$current_complate_file_list	
}

#执行远程命令
function exec_remote_command() {
        if [[ ${dic[remote_type]} = 'scp' ]];then
		exec_remote_scp_command
        else 
		echo '暂不支持其他传输方式'
		exit
	fi
}

#执行scp命令
function exec_remote_scp_command() {
	scp_server_name=${dic[scp_server_name]}
 	echo $scp_server_name	
	if [ -n "$scp_server_name" ]; then
		tmp_scp_server_name_list=(${scp_server_name//&/ })
		tmp_complate_file_list=(${dic[complate_file_path]//,/ })
		for tmp_file_item in ${tmp_complate_file_list[@]}
		   do
		     for tmp_scp_server in ${tmp_scp_server_name_list[@]}
			do
				server_info=(${tmp_scp_server//:/ })
        			ip=${server_info[0]}
        			user=${server_info[1]}
        			password=${server_info[2]}
        			path=${server_info[3]}
				#remote_command="scp  $tmp_file_item $user@$ip:$path/"
        			remote_command="scp  $tmp_file_item $user@$ip:$path/$(date +%Y-%m-%d)/"
expect << EOF
        			set timeout 300
        			spawn bash -c "$remote_command"
        			expect {
        				"yes/no" {send "yes\r"; exp_continue}
        				"password" {send "$password\r"}
       				 }
        			expect eof
EOF
			done
		   done
		
	fi
}
#执行创建文件夹信息
function exec_mkdir_command() {
	tmp_scp_server_name_list=(${scp_server_name//&/ })
	for tmp_scp_server in ${tmp_scp_server_name_list[@]}
           do
		 server_info=(${tmp_scp_server//:/ })
		 ip=${server_info[0]}
		 user=${server_info[1]}
		 password=${server_info[2]}
		 path=${server_info[3]}
		 path="$path/$(date +%Y-%m-%d)"
		 remote_command="ssh $user@$ip ' test -d $path && echo ok || mkdir -p $path'"
		echo $remote_command
expect << EOF
                                set timeout 300
                                spawn bash -c "$remote_command"
                                expect {
                                        "yes/no" {send "yes\r"; exp_continue}
                                        "password" {send "$password\r"}
                                 }
                                expect eof
EOF
                        done
}

#删除打包文件
function exec_delete_command() {
	tmp_complate_file_list=(${dic[complate_file_path]//,/ })
        for tmp_file_item in ${tmp_complate_file_list[@]}
	   do
		rm -rf "$tmp_file_item"
	   done
}

